﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BussinessEntity;
using Db4objects.Db4o;
using Db4objects.Db4o.Linq;
using Utilities;
using Configurations;

namespace DataAccessLayer
{
    public class ServiceDA
    {
        public ActionResult<Service> Insert(Service entity)
        {
            ActionResult<Service> rs = new ActionResult<Service>();
            using (var conn = DbConnector.OpenDB())
            {
                try
                {
                    var temp = (from Service p in conn
                                where p.ServiceID == entity.ServiceID
                                         select p).FirstOrDefault<Service>();
                    if (temp == null)
                    {
                        conn.Store(entity);
                        rs.Object = entity;
                    }
                    else
                        rs.AddException(new Exception("Trùng khóa chính"));
                }
                catch (Exception ex)
                {
                     rs.AddException(ex);
                }
                finally
                {
                    conn.Close();
                }
                return rs;
                
            }
        }

        public ActionResult<Service> Update(Service entity)
        {
            ActionResult<Service> rs = new ActionResult<Service>();

            using (var conn = DbConnector.OpenDB())
            {
                try
                {
                    var temp = (from Service p in conn
                                where p.ServiceID == entity.ServiceID
                                select p).FirstOrDefault<Service>();
                    if (temp != null)
                    {
                        temp.ServiceName = entity.ServiceName;
                        temp.Quantity = entity.Quantity;
                        temp.ServiceCategory = entity.ServiceCategory;
                        temp.UnitPrice = entity.UnitPrice;
                        conn.Store(temp);
                        rs.Object = temp;
                    }
                    else
                        rs.AddException(new Exception("Mã dịch vụ : " + entity.ServiceID + " không tồn tại."));
                }
                catch (Exception ex)
                {
                    rs.AddException(ex);
                }
                finally
                {
                    conn.Close();
                }
                return rs;
            }
        }

        public ActionResult<Service> GetSingle(string MaDV)
        {
            ActionResult<Service> rs = new ActionResult<Service>();
            using (var conn = DbConnector.OpenDB())
            {
                try
                {
                    var temp = (from Service p in conn
                                where p.ServiceID == MaDV
                                select p).FirstOrDefault<Service>();
                    if (temp != null)
                    {
                        rs.Object = temp;
                    }
                    else
                        rs.AddException(new Exception("Mã dịch vụ : " + MaDV + " không tồn tại."));
                }
                catch (Exception ex)
                {
                    rs.AddException(ex);
                }
                finally
                {
                    conn.Close();
                }
                return rs;
            }
        }

        public ActionResult<Service> Delete(string MaDV)
        {
            ActionResult<Service> rs = new ActionResult<Service>();
            using (var conn = DbConnector.OpenDB())
            {
                try
                {
                    var temp = (from Service p in conn
                                where p.ServiceID == MaDV
                                select p).ToList<Service>();
                    if (temp.Count!=0)
                    {
                        
                        var hd = (from ReceiptDetail p in conn
                                  where p.ServiceID == MaDV
                                  select p).FirstOrDefault<ReceiptDetail>();
                        if (hd == null)
                        {
                            var pn = (from ImportDetail p in conn
                                      where p.ServiceID == MaDV
                                      select p).FirstOrDefault<ImportDetail>();
                            if (pn == null)
                            {
                                foreach (var i in temp)
                                    conn.Delete(i);
                            }
                            else
                                rs.AddException(new Exception("Tồn tại một phiếu nhập sử dụng dịch vụ này"));
                        }
                        else
                            rs.AddException(new Exception("Tồn tại một hóa đơn sử dụng dịch vụ này"));
                    }
                    else
                        rs.AddException(new Exception("Mã dịch vụ : " + MaDV + " không tồn tại."));
                }
                catch (Exception exx)
                {
                    rs.AddException(exx);
                }
                finally
                {
                    conn.Close();
                }
                return rs;
            }
        }

        public ActionResult<Service> GetAll()
        {
            ActionResult<Service> rs = new ActionResult<Service>();
            using (var conn = DbConnector.OpenDB())
            {

                try
                {
                    rs.Array = (from Service p in conn
                                         select p).ToList<Service>();

                }
                catch (Exception ex)
                {
                    rs.AddException(ex);
                }
                finally
                {
                    conn.Close();
                }
                return rs;
            }
        }

        public ActionResult<Service> GetAllHardware()
        {
            ActionResult<Service> rs = new ActionResult<Service>();
            using (var conn = DbConnector.OpenDB())
            {

                try
                {
                    rs.Array = (from Service p in conn
                                where p.ServiceCategory == ServiceState.ThayPhanCung
                                select p).ToList<Service>();

                }
                catch (Exception ex)
                {
                    rs.AddException(ex);
                }
                finally
                {
                    conn.Close();
                }
                return rs;
            }
        }

        public string NextID()
        {
            string nextid = "";
            using (var conn = DbConnector.OpenDB())
            {

                try
                {
                    var temp = (from Service p in conn
                                orderby p.ServiceID descending
                                select p).FirstOrDefault<Service>();
                    if (temp == null)
                        return "DV00001";
                    int ma = 0;
                    try
                    {
                        ma = DataUtil.ToInt(temp.ServiceID.Substring(2)) + 1;
                    }
                    catch (Exception)
                    {
                        ma = 1;
                    }
                    nextid = "DV";
                    for (int i = 0; i < 5 - ma.ToString().Length; i++)
                        nextid += "0";
                    nextid += ma.ToString();
                    while (true)
                    {
                        temp = (from Service p in conn
                                where p.ServiceID == nextid
                                select p).FirstOrDefault<Service>();
                        if (temp == null)
                            break;
                        ma++;
                        nextid = "DV";
                        for (int i = 0; i < 5 - ma.ToString().Length; i++)
                            nextid += "0";
                        nextid += ma.ToString();
                    }
                }
                catch (Exception)
                {
                }
                finally
                {
                    conn.Close();
                }
            }
            return nextid;
        }
    }
}
